package com.know.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletSecurityElement;

import org.apache.catalina.User;
import org.apache.commons.lang.StringUtils;

import my.mvc.RequestContext;
import my.mvc.Annotation.JSONOutput;
import my.util.ResponseUtils;

import com.know.entity.Article;
import com.know.entity.Lables;
import com.know.entity.set_article_lable;
import com.know.service.ArticleService;
import com.know.service.ImageService;
import com.know.service.LableService;
import com.kzw.bean.Record;
import com.kzw.dao.support.Page;
import com.kzw.json.Jackson;

/**
 * 文章服务
 *
 */
public class ArticleAction{
	
	
	private ArticleService articleService=new ArticleService();
	private LableService lableService=new LableService();
	private ImageService imageService=new ImageService();
	/**
	 * 文章页面跳转服务界面
	 */
	public void into(RequestContext rc) throws ServletException, IOException {
		
		List<Lables> list=lableService.list();
		rc.reqAttr("lableList", list);
		System.out.println("跳转文章页面");
		rc.forward("/article.jsp");
	}
	/**
	 * 单篇文章跳转界面（加载文章，评论）
	 */
	public void detail(RequestContext rc) throws ServletException, IOException {
		
		String detail_id=rc.param("id");//获取文章ID
		/*进行文章查找并读取*/
		Article article=articleService.findById(Long.parseLong(detail_id));
		/*查找标签*/
		List<Lables> list=lableService.list();
		rc.reqAttr("article",article);
		rc.reqAttr("lableList", list);
		System.out.println("跳转文章页面");
		rc.forward("/detail.jsp");
	}
	/**
	 * 流加载列表（前端未调整 原创 精华 这样的分类未约定
	 * */
	public void list(RequestContext rc) throws ServletException, IOException {
		int pageNo = rc.param("page", 1);
		int pageSize = rc.param("limit", 10);
		String orderBy = rc.param("orderBy", "");
		String orderDir = rc.param("orderDir", "");
		String lable_id=rc.param("lable_id");
		
		Page<Article> page = new Page<>(pageNo, pageSize);
		if(StringUtils.isNotBlank(orderBy)) {
			page.setOrderBy(orderBy);
			page.setOrder(orderDir);
		}
		page.setPage_temp(lable_id);
		Record record = articleService.findPage(page);
		long count=record.getCount();
		page.setTotalCount((int)count);
		/*count在此为页数*/
		count=page.getTotalPages();
		record.setCount(count);
		
		// 输出JSON
		String json = Jackson.me().toJson(record);
		//System.out.println(json);
		ResponseUtils.renderJson(rc.response(), json);
	}
	/**
	 * 表格列表（前端未调整 原创 精华 这样的分类未约定
	 * */
	public void talist(RequestContext rc) throws ServletException, IOException {
		int pageNo = rc.param("page", 1);
		int pageSize = rc.param("limit", 5);
		String orderBy = rc.param("orderBy", "");
		String orderDir = rc.param("orderDir", "");
		
		Page<Article> page = new Page<>(pageNo, pageSize);
		if(StringUtils.isNotBlank(orderBy)) {
			page.setOrderBy(orderBy);
			page.setOrder(orderDir);
		}
		
		Record record = articleService.findPage(page);
		// 输出JSON
		String json = Jackson.me().toJson(record);
		//System.out.println(json);
		ResponseUtils.renderJson(rc.response(), json);
	}
	/**
	 * 点击排行榜读取文章(只读取10个）
	 */
	public void likelist(RequestContext rc) throws ServletException, IOException {
		
		
		List<Article> list =articleService.findTen();
		
		// 输出JSON
		String json = Jackson.me().toJson(list);
		//System.out.println(json);
		ResponseUtils.renderJson(rc.response(), json);
	}
	/**
	 * 站长推荐根据 站长推荐分类进行读取前10个 ，(时间排序未改)
	 */
	public void admlist(RequestContext rc) throws ServletException,IOException{

		List<Article> list =articleService.findadm(10);
		// 输出JSON
		String json = Jackson.me().toJson(list);
		//System.out.println(json);
		ResponseUtils.renderJson(rc.response(), json);
	}
	/**
	 * 根据文章ID读取（用于编辑）
	 */
	public void getone(RequestContext rc) throws ServletException,IOException{
		String id=rc.param("id");
		if(id!=null && !id.equals("")) {
			Article article = articleService.findById(Long.parseLong(id));
			rc.reqAttr("article", article);
			rc.forward("/admin/article_form.jsp");
		}
	
	}
	/**
	 * 保存文章
	 */
	public void save(RequestContext rc) throws ServletException,IOException{
		// 将请求参数自动包装成Bean对象
		Article article = rc.form(Article.class);
		//用户只有博主
		article.setUser_id(1);
		//读取标签ID
		String lab_id=rc.param("lable");
		String temp=rc.param("article_remark_content");
		System.out.println("remark:"+temp);
		long id=Long.parseLong(lab_id);
		article.getSal().setLable_id(id);
		//读取图片UUID字符串
		String uuid=rc.param("imgpic");
		//进行拆分
		String []uuids=uuid.split(",");
		System.out.println("图片的UUID："+uuid);
	
		//System.out.println("lable_id:"+id);	
		if(article.getArticle_id()==0) {
			// 增加
			long aid=articleService.save(article);
			if(uuids!=null)
				imageService.update(uuids, aid);
		} else {
			// 修改
			articleService.update(article);
			imageService.update(uuids, article.getArticle_id());
		}
		
		// 输出JSON
		String json = Jackson.me().toJson(new Record(true));
		ResponseUtils.renderJson(rc.response(), json);
		
	}
	/**
	 * 文章删除
	 * @throws IOException
	 */
	public void del(RequestContext rc) throws ServletException,IOException{
		long id = rc.param("id", 0L);
		articleService.del(id);
		
		// 返回JSON
		String json = Jackson.me().toJson(new Record(true));
		ResponseUtils.renderJson(rc.response(), json);
	
	}
	/**
	 * 批量删除
	 * */
	public void multiDel(RequestContext rc) throws ServletException, IOException {
		String ids = rc.param("ids");
		if(StringUtils.isNotBlank(ids)) {
			articleService.multiDel(ids);
		}		
		String json = Jackson.me().toJson(new Record(true));
		ResponseUtils.renderJson(rc.response(), json);
	}
	
	/**
	 *后台管理更改分类sort 
	 */
	public void changeSort(RequestContext rc) throws ServletException, IOException {
		String id = rc.param("id");
		String open=rc.param("open");
		String sort_va=rc.param("sort_va");
		System.out.println("id:"+id);
		System.out.println("open:"+open);
		if(StringUtils.isNotBlank(id)) {
			if(open.equals("false"))
				articleService.changeSort(Long.parseLong(id),Long.parseLong(sort_va));
			else
				articleService.addSort(Long.parseLong(id),Long.parseLong(sort_va));

		}		
		String json = Jackson.me().toJson(new Record(true));
		ResponseUtils.renderJson(rc.response(), json);			
	}
	
	
}
